package com.example.lawyer.databind.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.databinding.ObservableField;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.lawyer.factory.LocationTool;
import com.example.lawyer.jsondata.ProvinceData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.w3c.dom.Text;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.crypto.Cipher;


public class AccountPostDataBindModel {

    public ObservableField<String> pname = new ObservableField<>();//省
    public ObservableField<String> cname = new ObservableField<>();//市
    public ObservableField<String> aname = new ObservableField<>();//区县


    public ObservableField<Integer>  schedule = new ObservableField<>();//注册进度


    /**
     * 打开三级联动地区选择
     */
    public void onOpenPicker(View view) {
        Context context = view.getContext();
        StringBuffer stringBuffer = new StringBuffer();
        try {
            AssetManager assets = view.getContext().getAssets();
            InputStream open = assets.open("province.json");

            InputStreamReader inputStreamReader = new InputStreamReader(open);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String lines;
            while ((lines = bufferedReader.readLine()) != null) {
                stringBuffer.append(lines);
            }

            Gson gson = new Gson();
            ArrayList<ProvinceData> provinceData = gson.fromJson(stringBuffer.toString(), new TypeToken<ArrayList<ProvinceData>>() {
            }.getType());
            System.out.println(provinceData.size());
            System.out.println(provinceData.get(0).name);
            System.out.println(provinceData.get(0).city.size());
            System.out.println(provinceData.get(0).city.get(0).area.get(0));
            System.out.println("转化后的对象");
            //选项1
            final ArrayList<String> options1Items = new ArrayList<>();
            //选项2
            final ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
            //选项3
            final ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();



            for(int s=0;s<provinceData.size();s++)
            {
                ProvinceData datas = provinceData.get(s);
                String name = datas.name;
                options1Items.add(name);

                ArrayList<ProvinceData.cityobj> city = datas.city;
                ArrayList<String> pr1 = new ArrayList<>();


                ArrayList<ArrayList<String>> ar1s = new ArrayList<>();
                for(int y=0;y<city.size();y++)
                {
                    String name1 = city.get(y).name;
                    pr1.add(name1);

                    ArrayList<String> area = city.get(y).area;
                    System.out.println(area);

                    ar1s.add(area);

                }

                options2Items.add(pr1);
                options3Items.add(ar1s);
            }







            OptionsPickerView optionsPickerView = new OptionsPickerBuilder(
                    context, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    pname.set(options1Items.get(options1));
                    cname.set(options2Items.get(options1).get(options2));
                    aname.set(options3Items.get(options1).get(options2).get(options3));

                }
            })
                    .setTitleText("所在地区选择").setDividerColor(Color.BLACK)
                    .setTextColorCenter(Color.BLACK)
                    .setContentTextSize(14)
                    .setSubCalSize(12)
                    .setTitleSize(12)
                    .setTitleColor(Color.GRAY)
                    .isCenterLabel(true)
                    .setTitleBgColor(Color.WHITE)
                    .setSubmitColor(Color.BLACK)
                    .setCancelColor(Color.BLACK)
                    .isRestoreItem(true)
                    .setCyclic(false,false,false)
                    .setDividerColor(Color.TRANSPARENT)
                    .build();

            optionsPickerView.setPicker(options1Items,options2Items,options3Items);
            optionsPickerView.show();

        }catch (Exception e)
        {

        }
    }




    /**
     * 返回
     */
    public void turnbackClick(View view)
    {
        System.out.println("返回有效性");
        Intent intent = new Intent();
        intent.setAction("AccountLogin");
        view.getContext().startActivity(intent);
    }

    /**
     * 提交确认
     */
    public void commitSubtim(View view,int i)
    {
        System.out.println("确认点击"+i);
        int uu = i+1;
        this.schedule.set(uu);
        if(uu>4)
        {
            TextView textView = (TextView) view;
            textView.setVisibility(View.GONE);
        }

    }
}
